package net.xdclass.onlinexdclass.interceptor;


import lombok.extern.slf4j.Slf4j;
import net.xdclass.onlinexdclass.exception.XDException;
import net.xdclass.onlinexdclass.utils.IPWhiteListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 薛虎
 * @date 2021/1/4 9:48
 * IP白名单拦截器
 */
@Slf4j
public class IPLimitInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    IpWhile ipWhile;

    @Override
    public boolean preHandle(HttpServletRequest request,

                             HttpServletResponse response, Object handler) throws Exception {
        String ip = getIpAddress(request);

        //读取ip白名单配置文件ipwhite.properties
//        Properties properties = PropertiesLoaderUtils.loadAllProperties("ipwhitelist.properties");
        String ipWhilte = ipWhile.getList();


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("IP白名单是{},时间是{},访问Ip是{}", ipWhilte, simpleDateFormat.format(new Date()), ip);

        //判断请求ip地址 是否在白名单呢
        if (IPWhiteListUtil.checkLoginIP(ip, ipWhilte)) {
            return super.preHandle(request, response, handler);
        }

        throw new XDException(1, "IP非法访问");


    }

    //获取配置请求的ip地址

    private String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            log.info("访问接口服务的客户端IP是{},时间是{}", ip, simpleDateFormat.format(new Date()));

            if (ip.equals("127.0.0.1")) {

                //根据网卡取本机配置的IP

                InetAddress inet = null;

                try {

                    inet = InetAddress.getLocalHost();

                } catch (Exception e) {

                    e.printStackTrace();

                }

                ip = inet.getHostAddress();

            }

        }

        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ip != null && ip.length() > 15) { //"***.***.***.***".length() = 15

            if (ip.indexOf(",") > 0) {

                ip = ip.substring(0, ip.indexOf(","));

            }

        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("访问接口服务的客户端IP是{},时间是{}", ip, simpleDateFormat.format(new Date()));

        return ip;

    }
}
